package com.xidian.zhiancloud.controller.checkdanger;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelReadListener extends AnalysisEventListener<Map<Integer, String>> {

private static final int BATCH_COUNT = 1000;
	
	
	 //public UserService userservice;
	private static final Logger LOGGER = LoggerFactory.getLogger(ExcelReadListener.class);
    List<Map> list = new ArrayList<Map>();
	
	@Override
	public void invoke(Map<Integer, String> data, AnalysisContext context) {
		// TODO Auto-generated method stub
		//LOGGER.info("111解析到一条数据:{}", JSON.toJSONString(data));
		//LOGGER.info(context.readRowHolder().getCellMap().get(1)+"row:" + context.readRowHolder().getRowIndex() +" value: "+data.get(context.readRowHolder().getRowIndex()));
		
		Map<String,String> map = new HashMap<String,String>();
		map.put("row"+0, data.get(0));
		list.add(setStringMapData(data));
        if (list.size() >= BATCH_COUNT) {
        	//LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
            saveData();
            list.clear();
           /* try
            { 
    			Thread.sleep(2000); //两秒
            }
    		catch(InterruptedException e)
            { 
                e.printStackTrace(); 
            } */
        }
	}

	@Override
	public void doAfterAllAnalysed(AnalysisContext context) {
		// TODO Auto-generated method stub
		
		saveData();
		
		
		/*try
        { 
			Thread.sleep(2000); //两秒
        }
		catch(InterruptedException e)
        { 
            e.printStackTrace(); 
        } */
	}
	
	@Override
	public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
		// TODO Auto-generated method stub
		super.invokeHeadMap(headMap, context);
		System.out.println("表头信息："+headMap);
	}

	@Override
	public void onException(Exception exception, AnalysisContext context) throws Exception {
		// TODO Auto-generated method stub
		super.onException(exception, context);
		System.out.println("错误："+exception.getMessage());
	}

	private void saveData() {
        LOGGER.info("{}条数据，开始存储数据库！", list.size());
        
        //userservice.insertImportAssign(list);
        LOGGER.info("存储数据库成功！");
    }

	public Map<String,String> setStringMapData(Map<Integer, String> data)
	{
		Map<String,String> map = new HashMap<String,String>();
		map.put("filed1", data.get(0));
		map.put("filed2", data.get(1));
		map.put("filed3", data.get(2));
		map.put("filed4", data.get(3));
		map.put("filed5", data.get(4));
		map.put("filed6", data.get(5));
		map.put("filed7", data.get(6));
		map.put("filed8", data.get(7));
		map.put("filed9", data.get(8));
		map.put("filed10", data.get(9));
		map.put("filed11", data.get(10));

        map.put("filed12", data.get(11));
        map.put("filed13", data.get(12));
        map.put("filed14", data.get(13));
        map.put("filed15", data.get(14));
        map.put("filed16", data.get(15));
        map.put("filed17", data.get(16));
        map.put("filed18", data.get(17));
        map.put("filed19", data.get(18));
        map.put("filed20", data.get(19));
        map.put("filed21", data.get(20));

        map.put("filed22", data.get(21));
        map.put("filed23", data.get(22));
        map.put("filed24", data.get(23));
        map.put("filed25", data.get(24));

		return map;
	}
}
